package vista;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import com.toedter.calendar.JDateChooser;

import controlador.ControladorCompraProveedor;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class VentanaCompraProveedor extends javax.swing.JFrame {
	private JLabel lblProveedor;
	private JLabel lblCantidadU;
	private JComboBox cmbDeposito;
	private JLabel lblDeposito;
	private JPanel panUbicar;
	private JTextField txtCantidad;
	private JLabel lblCantidad;
	private JComboBox cmbProducto;
	private JComboBox cmbProveedor;
	private JLabel lblProducto;
	private JRadioButton btnEstante;
	private JTextField txtCodLote;
	private JDateChooser jDateChooser1;
	private JButton btnCancelarUb;
	private JTextField txtCantPend;
	private JLabel lblCantPend;
	private JTextField txtCantUb;
	private JButton btnFinalizar;
	private JLabel lblCodLote;
	private JLabel lblFechaVenc;
	private JButton btnProcesar;
	private JButton btnComprar;
	private JButton btnSalir;
	private JButton btnCancelar;
	private JTable tblProductos;
	private JButton btnUbicar;
	private JComboBox cmbNum;
	private JRadioButton btnPaleta;
	private JPanel jPanelRadioBtn;
	private JDateChooser jdFechaFacturaCompra;
	private JLabel lblNumeroFactura;
	private JTextField txtNumFactura;
	

	//funcionan los radio button
	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				VentanaCompraProveedor inst = new VentanaCompraProveedor();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public VentanaCompraProveedor() {
		super();
		initGUI();
	}

	public void agregarListener(ControladorCompraProveedor accion){

		btnFinalizar.addActionListener(accion);
		btnCancelar.addActionListener(accion);
		btnSalir.addActionListener(accion);
		btnProcesar.addActionListener(accion);
		btnCancelar.addActionListener(accion);
		btnUbicar.addActionListener(accion);
		btnCancelarUb.addActionListener(accion);
		btnEstante.addItemListener(accion);
		btnPaleta.addItemListener(accion);

	}

	public void setCantidad(int can){
		txtCantidad.setText(String.valueOf(can));
	}

	public int getCantidad(){
		return Integer.valueOf(txtCantidad.getText());
	}

	public String getNombreProducto(){	
		return cmbProducto.getSelectedItem().toString();
	}

	public Date getFecha(){
		return jDateChooser1.getDate();
	}

	public String getNumeroContenedor(){

		return cmbNum.getSelectedItem().toString();
	}


	public String getCodigoDeposito(){
		return cmbDeposito.getSelectedItem().toString();
	}


	public void setCantidadUb(int cub){
		txtCantUb.setText(String.valueOf(cub));
	}

	public int getCantidadUb(){
		return Integer.valueOf(txtCantUb.getText());
	}

	public void setCantidadPen(int cpen){
		txtCantPend.setText(String.valueOf(cpen));
	}

	public int getCantidadPen(){
		return Integer.valueOf(txtCantPend.getText());
	}

	public void setCodLote(int cpen){
		txtCodLote.setText(String.valueOf(cpen));
	}
	
	public void setCodLoteVacio(){
		txtCodLote.setText("");
	}


	public String getCodLote(){
		return txtCodLote.getText();
	}

	public void setTablaProducto(String tbl){
		tblProductos.setTableHeader(null);
	}

	public String getTablaProducto(){
		return String.valueOf(tblProductos);
	}

	public void mostrarMensaje(String mensaje) {
		JOptionPane.showMessageDialog(this, mensaje);
	}

	public void setProveedoresComboBox(ArrayList<String> nombre){
		cmbProveedor.removeAllItems();
		cmbProveedor.addItem("Seleccione una opcion");
		for(int i = 0; i < nombre.size();i++)
			cmbProveedor.addItem(nombre.get(i));
	}

	public void setProductosComboBox(ArrayList<String> nombre){
		cmbProducto.removeAllItems();
		cmbProducto.addItem("Seleccione una opcion");
		for(int i = 0; i < nombre.size();i++)
			cmbProducto.addItem(nombre.get(i));
	}

	public void setDepositosComboBox(ArrayList<String> nombre){
		cmbDeposito.removeAllItems();
		cmbDeposito.addItem("Seleccione una opcion");
		for(int i = 0; i < nombre.size();i++)
			cmbDeposito.addItem(nombre.get(i));
	}

	public void limpiarCampos(){
		txtCantidad.setText("");
		jDateChooser1.setToolTipText("");	
	}
	
	public void limpiarCamposUbicar(){
		txtCantUb.setText("");
	}
	
	public void habilitarUbicar(){
		cmbDeposito.setEnabled(true);
		txtCantUb.setEnabled(true);
		cmbNum.setEnabled(true);
		btnUbicar.setEnabled(true);
		btnCancelarUb.setEnabled(true);
	}

	public void habilitarDistribuir(){
		txtCantidad.setEnabled(true);
		btnProcesar.setEnabled(true);
		cmbProducto.setEnabled(true);
		jDateChooser1.setEnabled(true);
	}
	
	public void deshabilitarDistribuir(){
		txtCantidad.setEnabled(false);
		btnProcesar.setEnabled(false);
		cmbProducto.setEnabled(false);
		cmbProveedor.setEnabled(false);
		jDateChooser1.setEnabled(false);
		jdFechaFacturaCompra.setEnabled(false);
		txtNumFactura.setEnabled(false);
		
	}
	
	public void deshabilitarUbicar(){
		cmbDeposito.setEnabled(false);
		txtCantUb.setEnabled(false);
		cmbNum.setEnabled(false);
		btnUbicar.setEnabled(false);
		btnCancelarUb.setEnabled(false);
		
	}

	public void setListado(AbstractTableModel abstractTableModel){
		tblProductos.setModel(abstractTableModel);
	}



	public int getOpcion(){
		if(btnEstante.isSelected())
			return 1;
		else
			return 2;
	}

	public void setOpcion(int opcion){
		if(opcion == 1)
			btnPaleta.setSelected(true);
		else
			btnEstante.setSelected(true);
	}


	public JRadioButton getRbtPaleta() {
		return btnPaleta;
	}

	public JRadioButton getRbtEstante() {
		return btnEstante;
	}

	public void llenarComboPaleta(int numero){
		cmbNum.removeAllItems();
		cmbNum.addItem("Seleccione una opcion");
		for(int i = 1; i <= numero; i++)
			cmbNum.addItem(i);
	} 

	public void llenarComboEstante(int numero){
		cmbNum.removeAllItems();
		cmbNum.addItem("Selecione una opcion");
		for (int i = 1; i <= numero; i++) {
			cmbNum.addItem(i);
		}
	}

	public Date getFechaFactura(){
		return jdFechaFacturaCompra.getDate();
	}
	
	public void setFechaFactura(){

	}
	
	public int getCmbDeposito(){
		return Integer.parseInt((String)cmbDeposito.getSelectedItem());
	}
	public String getCmbDepositoString(){
		return  cmbDeposito.getSelectedItem().toString();
	}
	
	public void setProveedor(String prov){
		cmbProveedor.setSelectedItem(prov);
	}

	public String getProveedor(){	
		return cmbProveedor.getSelectedItem().toString();
	}

	public void setDeposito(String dep){
		cmbDeposito.setSelectedItem(dep);
	}
	
	public void setTxtNumeroFactura(String num){
		this.txtNumFactura.setText(num);
	}
	
	public String getTxtNumFactura(){
		return this.txtNumFactura.getText();
	}

	
	
	public String getDeposito(){	
		return cmbDeposito.getSelectedItem().toString();
	}

	public void setContenedor(String con){
		cmbNum.setSelectedItem(con);
	}

	public String getContenedor(){	
		return cmbNum.getSelectedItem().toString();
	}



	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setTitle("Compra a Proveeedores");
			{
				lblProveedor = new JLabel();
				getContentPane().add(lblProveedor);
				lblProveedor.setText("Proveedor:");
				lblProveedor.setBounds(39, 47, 81, 15);
			}
			{
				lblProducto = new JLabel();
				getContentPane().add(lblProducto);
				lblProducto.setText("Producto:");
				lblProducto.setBounds(39, 74, 74, 17);
			}
			{
				ComboBoxModel cmbProveedorModel = 
						new DefaultComboBoxModel(
								new String[] { });
				cmbProveedor = new JComboBox();
				getContentPane().add(cmbProveedor);
				cmbProveedor.setModel(cmbProveedorModel);
				cmbProveedor.setBounds(231, 43, 123, 22);
			}
			{
				ComboBoxModel cmbProductoModel = 
						new DefaultComboBoxModel(
								new String[] { });
				cmbProducto = new JComboBox();
				getContentPane().add(cmbProducto);
				cmbProducto.setModel(cmbProductoModel);
				cmbProducto.setBounds(231, 71, 123, 22);
			}
			{
				lblCantidad = new JLabel();
				getContentPane().add(lblCantidad);
				lblCantidad.setText("Cantidad:");
				lblCantidad.setBounds(39, 103, 88, 15);
			}
			{
				txtCantidad = new JTextField();
				getContentPane().add(txtCantidad);
				txtCantidad.setBounds(233, 95, 39, 22);
				txtCantidad.addKeyListener(new KeyAdapter() {
					public void keyTyped(KeyEvent evt) {
						char car = evt.getKeyChar();
						if(txtCantidad.getText().length()>=8) evt.consume();
						if((car<'0' || car>'9')) evt.consume();
						
							//TODO add your code for txtCedulaCliente.keyTyped
						}
					});
				
			}
			{
				panUbicar = new JPanel();
				getContentPane().add(panUbicar);
				panUbicar.setBounds(24, 187, 519, 158);
				panUbicar.setLayout(null);
				panUbicar.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
				{
					lblDeposito = new JLabel();
					panUbicar.add(lblDeposito);
					lblDeposito.setText("Deposito:");
					lblDeposito.setBounds(14, 45, 71, 15);
				}
				{
					ComboBoxModel cmbDepositoModel = 
							new DefaultComboBoxModel(
									new String[] { });
					cmbDeposito = new JComboBox();
					panUbicar.add(cmbDeposito);
					cmbDeposito.setModel(cmbDepositoModel);
					cmbDeposito.setBounds(217, 40, 79, 24);
				}
				{
					lblCantidadU = new JLabel();
					panUbicar.add(lblCantidadU);
					lblCantidadU.setText("Cantidad a Ubicar:");
					lblCantidadU.setBounds(14, 72, 191, 15);
				}
				{
					ComboBoxModel cmbNumModel = 
							new DefaultComboBoxModel(
									new String[] { "", "" });
					cmbNum = new JComboBox();
					panUbicar.add(cmbNum);
					cmbNum.setModel(cmbNumModel);
					cmbNum.setBounds(217, 97, 79, 22);
				}
				{
					btnUbicar = new JButton();
					panUbicar.add(btnUbicar);
					btnUbicar.setText("Ubicar");
					btnUbicar.setBounds(31, 131, 106, 22);
				}
				{
					txtCantUb = new JTextField();
					panUbicar.add(txtCantUb);
					txtCantUb.setBounds(217, 71, 38, 22);
					txtCantUb.addKeyListener(new KeyAdapter() {
						public void keyTyped(KeyEvent evt) {
							char car = evt.getKeyChar();
							if(txtCantUb.getText().length()>=8) evt.consume();
							if((car<'0' || car>'9')) evt.consume();
							
								//TODO add your code for txtCedulaCliente.keyTyped
							}
						});
				}
				{
					lblCantPend = new JLabel();
					panUbicar.add(lblCantPend);
					lblCantPend.setText("Cantidad Pendiente por Ubicar:");
					lblCantPend.setBounds(12, 9, 253, 15);
				}
				{
					txtCantPend = new JTextField();
					panUbicar.add(txtCantPend);
					txtCantPend.setBounds(272, 6, 39, 22);
					txtCantPend.setEnabled(false);
				}
				{
					btnCancelarUb = new JButton();
					panUbicar.add(btnCancelarUb);
					btnCancelarUb.setText("Cancelar Ubicacion");
					btnCancelarUb.setBounds(292, 131, 145, 22);
				}
				{
					lblCodLote = new JLabel();
					panUbicar.add(lblCodLote);
					lblCodLote.setText("Codigo Lote:");
					lblCodLote.setBounds(329, 8, 108, 16);
				}
				{
					txtCodLote = new JTextField();
					panUbicar.add(txtCodLote);
					txtCodLote.setBounds(437, 6, 68, 22);
					txtCodLote.setEnabled(false);
				}
				{
				ButtonGroup rBtnGroup = new ButtonGroup();
				
				btnPaleta = new JRadioButton("Paleta");
				btnPaleta.setActionCommand("Paleta");
				btnPaleta.setSelected(true);
			
				btnEstante = new JRadioButton("Estante");
				btnEstante.setActionCommand("Estante");
			
				rBtnGroup.add(btnPaleta);
				rBtnGroup.add(btnEstante);
				}	
				{
					jPanelRadioBtn = new JPanel();
					GroupLayout jPanelRadioBtnLayout = new GroupLayout((JComponent)jPanelRadioBtn);
					jPanelRadioBtn.setLayout(jPanelRadioBtnLayout);
					panUbicar.add(jPanelRadioBtn);
					jPanelRadioBtn.setBounds(14, 93, 191, 32);	
					jPanelRadioBtnLayout.setHorizontalGroup(jPanelRadioBtnLayout.createSequentialGroup()
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(btnPaleta, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(btnEstante, 0, 82, Short.MAX_VALUE)
						.addContainerGap());
					jPanelRadioBtnLayout.setVerticalGroup(jPanelRadioBtnLayout.createSequentialGroup()
						.addGap(6)
						.addGroup(jPanelRadioBtnLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
						    .addComponent(btnPaleta, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
						    .addComponent(btnEstante, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
						.addContainerGap());
				}
			}
			{
				TableModel tblProductosModel = 
						new DefaultTableModel(
								//Mosca Aqui..!!
								new String[][] { { "Producto", "Cantidad","Fecha Vencimiento" }, { "", "" } },
								new String[] { "Column 1", "Column 2","Column 3" });
				tblProductos = new JTable();
				getContentPane().add(tblProductos);
				tblProductos.setModel(tblProductosModel);
				tblProductos.setBounds(16, 357, 519, 117);
				tblProductos.getTableHeader().setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
				tblProductos.setEnabled(false);
			}
			{
				btnFinalizar = new JButton();
				getContentPane().add(btnFinalizar);
				btnFinalizar.setText("Almacenar");
				btnFinalizar.setBounds(24, 480, 108, 22);
			}
			{
				btnCancelar = new JButton();
				getContentPane().add(btnCancelar);
				btnCancelar.setText("Cancelar");
				btnCancelar.setBounds(220, 480, 106, 22);
			}
			{
				btnSalir = new JButton();
				getContentPane().add(btnSalir);
				btnSalir.setText("Salir");
				btnSalir.setBounds(424, 480, 106, 22);
			}
			{
				btnProcesar = new JButton();
				getContentPane().add(btnProcesar);
				btnProcesar.setText("Distribuir");
				btnProcesar.setBounds(40, 160, 123, 22);
			}
			{
				lblFechaVenc = new JLabel();
				getContentPane().add(lblFechaVenc);
				lblFechaVenc.setText("Fecha de Vencimiento:");
				lblFechaVenc.setBounds(36, 130, 185, 15);
			}
			{
				jDateChooser1 = new JDateChooser();
				getContentPane().add(jDateChooser1);
				jDateChooser1.setBounds(233, 123, 123, 22);
				jDateChooser1.setVisible(true);
			}
			{
				txtNumFactura = new JTextField();
				getContentPane().add(txtNumFactura);
				txtNumFactura.setBounds(231, 12, 115, 22);
			}
			{
				lblNumeroFactura = new JLabel();
				getContentPane().add(lblNumeroFactura);
				lblNumeroFactura.setText("Numero de Factura:");
				lblNumeroFactura.setBounds(39, 15, 125, 15);
			}
			{
				jdFechaFacturaCompra = new JDateChooser();
				getContentPane().add(jdFechaFacturaCompra);
				jdFechaFacturaCompra.setBounds(392, 12, 119, 22);
			}
			pack();
			this.setSize(557, 543);
		} catch (Exception e) {
			//add your error handling code here
			e.printStackTrace();
		}
	}

}
